package org.example.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.common.config.CaffeineConfig;
import org.example.dto.wechat.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 公众号工具类
 */
@Slf4j
@Component
public class PublicWechatUtils {

    @Value("${pubilcWechat.miniapp.appid}")
    private String appId;

    @Value("${pubilcWechat.miniapp.secret}")
    private String secret;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private CaffeineCacheUtil caffeineCacheUtil;
    private String ACCESS_TOKEN_API="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
    /**
     * 获取文章
     */
    private String BATCHGET_MATERIAL_API="https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s";
    /**
     * 获取接口token
     * @return
     */
    public GetAccessTokenResultDTO getAccessToken(){
        Object object = caffeineCacheUtil.getCache(CaffeineConfig.CACHE_NAMES,"AccessToken");
        String accessToken="";
        if(ObjectUtil.isNull(object)){
            String url = String.format(ACCESS_TOKEN_API,appId, secret);
            ResponseEntity<String> forEntity = this.restTemplate.getForEntity(url, String.class);
            String body = forEntity.getBody();
            log.info("返回结果：{}",body);
            if(StrUtil.isNotBlank(body)){
                GetAccessTokenResultDTO result = JSONObject.parseObject(body,GetAccessTokenResultDTO.class);
                accessToken=result.getAccess_token();
                caffeineCacheUtil.putCache(CaffeineConfig.CACHE_NAMES,"AccessToken",accessToken);
            }
        }else{
            accessToken=object.toString();
        }
        return GetAccessTokenResultDTO.builder().access_token(accessToken).build();
    }

    /**
     * 获取文章
     * @param paramsDTO
     * @return
     */
    public GetBatchgetMaterialResultDTO getBatchgetMaterial(GetBatchgetMaterialParamsDTO paramsDTO){
        GetAccessTokenResultDTO resultDTO=  this.getAccessToken();
        if(ObjectUtil.isNotNull(resultDTO)){
            String url = String.format(BATCHGET_MATERIAL_API,resultDTO.getAccess_token());
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.add(HttpHeaders.CONTENT_TYPE,"application/json;charset=UTF-8");
            HttpEntity http=new HttpEntity(paramsDTO,httpHeaders);
            ResponseEntity<String> forEntity = this.restTemplate.postForEntity(url, http,String.class);
            String body = forEntity.getBody();
//            log.info("返回结果：{}",body);
            if(StrUtil.isNotBlank(body)){
                GetBatchgetMaterialResultDTO result = JSONObject.parseObject(body,GetBatchgetMaterialResultDTO.class);
                return result;
            }
        }
        return null;
    }
}
